/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pandora.tv;

import android.app.WallpaperManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.test.UiThreadTest;
import android.view.Display;

import com.pandora.tv.Info.ApplicationInfo;
import com.pandora.tv.view.TabPageIndicator;
import com.pandora.tv.view.TabPagerAdapter;

/**
 * Default launcher application.
 */
public final class Launcher extends FragmentActivity  {

	static final String TAG = "Launcher";

	FocusLayer mFocusLayer = null;

	private ViewPager mPager;
	private TabPageIndicator mIndicator;

	private PagerAdapter mAdapter;
	Context mContext;
	
	private LauncherModel mModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		sLog.i(TAG, "onCreate start");
		mContext = this;

		mModel = ((LauncherApplication)getApplication()).getModel();

		new Handler() {
			@Override
			public void handleMessage(Message msg) {

				mModel.Initialization();

			}
		}.sendEmptyMessageDelayed(0, 1 * 1000);
		
		
		Handler han = new Handler() {
			int count = 0;
			@Override
			public void handleMessage(Message msg) {
				count++;
				
				switch (msg.what) {
				case 0:
					mModel.onExeApplication(null);
					if ( count < 2 )
						sendEmptyMessageDelayed(0, 5* 1000);
					else 
						sendEmptyMessage(1);
					break;
				case 1:
					mModel.onRemoveApplication(null);
					sendEmptyMessageDelayed(2, 3* 1000);
					break;
				case 2:
					mModel.onRemoveHistory(null, false, true);

				default:
					break;
				}
				
				

			}
		};
		han.sendEmptyMessageDelayed(0, 5* 1000);
		
		setWallpaperDimension();
		setContentView(R.layout.launcher);
		setupViews();
		sLog.i(TAG, "onCreate end");
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		
		if ( null != mModel ) 
			mModel.onReleaseResource();

		
		android.os.Process.killProcess(android.os.Process.myPid());
		
		super.onDestroy();
	}


	private void setupViews() {
		sLog.i(TAG, "setupViews LoopTabPageAdapter");

		mAdapter = new TabPagerAdapter(mContext, getSupportFragmentManager());

		sLog.i(TAG, "setupViews ViewPager");
		mPager = (ViewPager)findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		sLog.i(TAG, "setupViews TabPageIndicator");
		mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);

		sLog.i(TAG, "setupViews setupViews End");
	}

	static final int WALLPAPER_SCREENS_SPAN = 1;

	private void setWallpaperDimension() {
		WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);

		Display display = getWindowManager().getDefaultDisplay();
		boolean isPortrait = display.getWidth() < display.getHeight();

		final int width = isPortrait ? display.getWidth() : display.getHeight();
		final int height = isPortrait ? display.getHeight() : display.getWidth();
		wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
	}
}
